package com.ars.flight;

import com.ars.db.DBTools;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DeleteFlightController implements Initializable {
    DBTools db;
    int t;
    @FXML
    private ScrollPane scrollPane;
    private VBox vBox = new VBox();
    private ObservableList<Row> rows = FXCollections.observableArrayList();

    @FXML
    public void commitAction() {
        db = new DBTools();
        int n = rows.size();
        for (int i = 0; i < n; i++) {
            Row row = rows.get(i);
            if (row.isSelected()) {
                t = db.update("delete from flight where fid=" + row.getNumber());

                vBox.getChildren().remove(2 * i);        //从vbox中删除一个row,
                vBox.getChildren().remove(2 * i);        //从vbox中删除下面的line
                rows.remove(i);                            //从rows中移除被删除的row
                i = i - 1;
                n = n - 1;
            }
        }
        if (t != 0) {
            JOptionPane.showMessageDialog(null, "删除成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
        }

    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        String sql = "select * from flight";
        db = new DBTools();
        ResultSet rs = db.query(sql);
        try {
            while (rs.next()) {

                Flight flight = new Flight();
                flight.setFid(rs.getInt("fid"));
                flight.setFirst_seats_remain(rs.getInt("first_seats_remain"));
                flight.setBusiness_seats_remain(rs.getInt("business_seats_remain"));
                flight.setEconomy_seats_remain(rs.getInt("economy_seats_remain"));
                flight.setDiscount(rs.getInt("discount"));
                flight.setDate(rs.getString("date"));
                flight.setSid(rs.getInt("sid"));
                Row row = new Row(flight);
                vBox.getChildren().addAll(row, new Line(0, 0, 640, 0));
                rows.add(row);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.close();
        }

        scrollPane.setContent(vBox);
    }

    class Row extends HBox {
        private CheckBox checkBox = new CheckBox();
        private Label fid = new Label();
        private Label first_seats_remain = new Label();
        private Label business_seats_remain = new Label();
        private Label economy_seats_remain = new Label();
        private Label discount = new Label();
        private Label date = new Label();
        private Label sid = new Label();

        public Row(Flight flight) {
            super();
            fid.setText("" + flight.getFid());
            first_seats_remain.setText("" + flight.getFirst_seats_remain());
            business_seats_remain.setText("" + flight.getBusiness_seats_remain());
            economy_seats_remain.setText("" + flight.getEconomy_seats_remain());
            discount.setText("" + flight.getDiscount());
            date.setText(flight.getDate());
            sid.setText("" + flight.getSid());
            checkBox.setPrefWidth(50);
            HBox.setMargin(checkBox, new Insets(5, 0, 0, 15));
            fid.setPrefWidth(55);
            fid.setAlignment(Pos.CENTER);
            HBox.setMargin(fid, new Insets(5, 0, 0, 0));
            first_seats_remain.setPrefWidth(105);
            first_seats_remain.setAlignment(Pos.CENTER);
            HBox.setMargin(first_seats_remain, new Insets(5, 0, 0, 0));
            business_seats_remain.setPrefWidth(110);
            business_seats_remain.setAlignment(Pos.CENTER);
            HBox.setMargin(business_seats_remain, new Insets(5, 0, 0, 0));
            economy_seats_remain.setPrefWidth(100);
            economy_seats_remain.setAlignment(Pos.CENTER);
            HBox.setMargin(economy_seats_remain, new Insets(5, 0, 0, 0));
            discount.setPrefWidth(50);
            discount.setAlignment(Pos.CENTER);
            HBox.setMargin(discount, new Insets(5, 0, 0, 0));
            date.setPrefWidth(70);
            date.setAlignment(Pos.CENTER);
            HBox.setMargin(date, new Insets(5, 0, 0, 0));
            sid.setPrefWidth(80);
            sid.setAlignment(Pos.CENTER);
            HBox.setMargin(sid, new Insets(5, 0, 0, 0));


            getChildren().addAll(checkBox, fid, first_seats_remain, business_seats_remain, economy_seats_remain, discount, date, sid);


        }

        public boolean isSelected() {
            return checkBox.isSelected();
        }

        public String getNumber() {
            return fid.getText();
        }
    }

}
